package com.qnbc.mystery.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import com.qnbc.mystery.pojo.domain.Collect;
import com.qnbc.mystery.pojo.domain.ProductSwiperImage;
import com.qnbc.mystery.pojo.param.PageModel;
import com.qnbc.mystery.service.ICollectService;
import com.qnbc.mystery.service.IProductSwiperImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qnbc.mystery.pojo.domain.FoodsProduct;
import com.qnbc.mystery.service.IProductService;

/**
 * 商品配置
 *
 * @author guzj
 * @date 2023-12-27
 */
@RestController
@Api("商城列表")
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ICollectService collectService;
    @Autowired
    private IProductSwiperImageService productSwiperImageService;


    //统计
    @GetMapping("/getProductCount")
    public AjaxResult getProductCount() {
        Map<String, Object> map = new HashMap<>();
        List<FoodsProduct> list = productService.list();
        long count = list.stream().filter(w -> "1".equals(w.getProStatus())).count();
        long count1 = list.stream().filter(w -> "2".equals(w.getProStatus())).count();
        map.put("upCount", count);
        map.put("downCount", count1);
        map.put("total", list.size());
        return AjaxResult.success(map);
    }


    /*
     * 关键字搜索
     * */
    @GetMapping("/search")
    public AjaxResult search(String q) {
        List<FoodsProduct> foodsProductList = productService.list(new QueryWrapper<FoodsProduct>().like("name", q));
        Map<String, Object> map = new HashMap<>();
        map.put("message", foodsProductList);
        return AjaxResult.success(map);
    }

    /*
     * 根据id查询商品详情
     * */
    @GetMapping("/detail")
    public AjaxResult detail(Integer id) {
        FoodsProduct foodsProduct = productService.getById(id);
        if (null != foodsProduct.getDiscountNum()) {
            foodsProduct.setDiscountPrice(foodsProduct.getDiscountNum().multiply(foodsProduct.getPrice()).divide(new BigDecimal(100)));
        } else {
            foodsProduct.setDiscountPrice(foodsProduct.getPrice());
        }
        LambdaQueryWrapper<Collect> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Collect::getUserid, "1");
        queryWrapper.eq(Collect::getProductid, id);
        Collect one = collectService.getOne(queryWrapper);
        if (null != one) {
            foodsProduct.setCollect(1);
        } else {
            foodsProduct.setCollect(2);// 为收藏
        }
        List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(new QueryWrapper<ProductSwiperImage>().eq("productid", foodsProduct.getId()).orderByAsc("sort"));
        foodsProduct.setProductSwiperImageList(productSwiperImageList);
        Map<String, Object> map = new HashMap<>();
        map.put("message", foodsProduct);
        return AjaxResult.success(map);
    }


    /*
     * 查询轮播商品
     * */
    @GetMapping("/findSwiper")
    public AjaxResult findSwiper() {
        List<FoodsProduct> swiperFoodsProductList = productService.list(new QueryWrapper<FoodsProduct>().eq("isSwiper", true).orderByAsc("swiperSort"));
        Map<String, Object> map = new HashMap<>();
        map.put("message", swiperFoodsProductList);
        return AjaxResult.success(map);
    }


    /*
     * 查询热门推荐4个
     * */
    @GetMapping("/findHot")
    public AjaxResult findHot() {
        Page<FoodsProduct> page = new Page<>(0, 8);
        Page<FoodsProduct> pageProduct = productService.page(page, new QueryWrapper<FoodsProduct>().eq("isHot", true).orderByAsc("hotDateTime"));
        List<FoodsProduct> hotFoodsProductList = pageProduct.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("message", hotFoodsProductList);
        return AjaxResult.success(map);
    }

    /**
     * 查询商品列表
     */

    @GetMapping("/list")
    @ApiOperation("商城列表")
    public AjaxResult list( FoodsProduct foodsProduct, PageModel pageModel) {
        PageHelper.startPage(foodsProduct.getPageNum(), foodsProduct.getPageSize());
        List<FoodsProduct> list = productService.selectProductList(foodsProduct);
//        list.forEach(w -> {
//            if (null != w.getDiscountNum()) {
//                w.setDiscountPrice(w.getDiscountNum().multiply(w.getPrice()).divide(new BigDecimal(100)));
//            } else {
//                w.setDiscountPrice(w.getPrice());
//            }
//        });
        PageInfo<FoodsProduct> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }


    /**
     * 查询当前盒子为绑定的商品列表
     */
    @ApiOperation("查询当前盒子为绑定的商品列表")
    @GetMapping("/listByBox")
    public TableDataInfo listByBox(FoodsProduct foodsProduct) {
        startPage();
        List<FoodsProduct> list = productService.selectProductListByBox(foodsProduct);
        //折扣价
//        list.forEach(w -> {
//            if (null != w.getDiscountNum()) {
//                w.setDiscountPrice(w.getDiscountNum().multiply(w.getPrice()).divide(new BigDecimal(100)));
//            } else {
//                w.setDiscountPrice(w.getPrice());
//            }
//        });
        return getDataTable(list);
    }



    /**
     * 导出商品列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodsProduct foodsProduct) {
        List<FoodsProduct> list = productService.selectProductList(foodsProduct);
        ExcelUtil<FoodsProduct> util = new ExcelUtil<FoodsProduct>(FoodsProduct.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息以及关联的规格
     */
    @ApiOperation("获取商品详细信息以及关联的规格")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productService.selectProductById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FoodsProduct foodsProduct) {
        return toAjax(productService.insertProduct(foodsProduct));
    }

    /**
     * 修改商品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody FoodsProduct foodsProduct) {
        return toAjax(productService.updateProduct(foodsProduct));
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
